package com.arch.dev.client.view;

import com.google.gwt.user.client.Window;

import com.google.gwt.user.client.ui.HTML;

import com.google.gwt.user.client.ui.HorizontalPanel;

import com.google.gwt.user.client.ui.Image;

import com.google.gwt.user.client.ui.PopupPanel;

import com.google.gwt.user.client.ui.VerticalPanel;

public class MakeBusy extends PopupPanel {

	private static MakeBusy busy = new MakeBusy();

	public static final String loading = "images/loader.gif";

	private HTML message = new HTML("");

	private boolean b;

	private MakeBusy() {

		init();

	};

	public static MakeBusy getInstance() {

		return busy;

	}

	private void init() {

		setAnimationEnabled(true);

		setAutoHideEnabled(false);

		setModal(true);

		VerticalPanel p = new VerticalPanel();

		p.setSize("100%", "100%");

		HorizontalPanel messageContainer = new HorizontalPanel();

		p.add(new HTML("<BR>"));

		Image progressImage = new Image(loading);

		messageContainer.add(progressImage);

		messageContainer.add(message);

		messageContainer.setCellVerticalAlignment(message,
				VerticalPanel.ALIGN_BOTTOM);

		p.add(messageContainer);

		p.setCellHorizontalAlignment(messageContainer,
				HorizontalPanel.ALIGN_CENTER);

		p.setCellVerticalAlignment(messageContainer, VerticalPanel.ALIGN_MIDDLE);

		p.add(new HTML("<BR>"));

		setWidget(p);

	}

	public void show()

	{

		int ph = getOffsetHeight();

		int pw = getOffsetWidth();

		int wh = Window.getClientHeight();

		int ww = Window.getClientWidth();

		int locx = (ww - pw - getOffsetWidth()) / 2;

		int locy = (wh - ph - getOffsetHeight()) / 2;

		setPopupPosition(locx, locy);

		super.show();

	}

	public void makeBusy(boolean busy, String messageText)

	{

		this.b = busy;

		if (busy == true)

		{

			message.setHTML("<h4>&nbsp;&nbsp;&nbsp;" + messageText + "...</h4>");

			show();

		}

		else

		{

			hide(true);

		}

	}

	public void makeBusy(boolean busy)

	{

		this.b = busy;

		if (busy == true)

		{

			// message.setHTML("<H4>&nbsp;&nbsp;&nbsp;Operation in progress ... </H4>");

			show();

		}

		else

		{

			hide(true);

		}

	}

}
